<?php 
/**
 * 大话水浒后台
 */
class Module_overlord{
	/**
	 * 结构函数
	 */
	function __construct(){
	
	}
	/**
	 * 显示后台武将
	 * Enter description here ...
	 */
	function index(){
		$time = time();
		$smarty = loadSmarty();
		loadLib('App_Overlord_Query');
		/**********************查询所有武将**********************/
		$list = App_Overlord_Query::allCollocate('app_overlord_generals');
		//技能列表
		loadLib('App_Overlord_Collocate');   
		$data = App_Overlord_Collocate::getSkill();
		$smarty->assign('data' , $data);
		$smarty->assign('list' , $list);
		
		return $smarty->fetch('overlord/index.tpl.php');
	}
	/**
	 * 一键取小括号
	 */
	function onekey(){
		loadLib('App_Overlord_Query');
		/**********************查询所有武将**********************/
		$list = App_Overlord_Query::allCollocate('app_overlord_generals');
		if (!empty($list)){
			foreach ($list as $k => $row) {
				$name = str_replace("（" , "(" , $row['Value']['name']);
				$name = str_replace("）" , ")" , $name);
				$row['Value']['name'] = $name; 
				$code = App_Overlord_Query::alterCollocate('app_overlord_generals', $row['Key'], $row['Value']);
			}
		}
		header('Location: ' . ADMINURL . '/?m=overlord');
	}
	/**
	 * 昵称同步
	 */
	function synchro(){
		loadLib('App_Overlord_GameStart');
		/**********************查询所有玩家**********************/
		$list = App_Overlord_GameStart::getAllOverlord('app_overlord_user');
		if (!empty($list)){
			foreach ($list as $k => $row) {
				if (empty($row['Value']['name'])){
					$key[] = ltrim($row['Key'] , 'user_');
				}
			}
		}
		if (!empty($key)){
			loadLib('User');
			$user_info = User::getBaseInfo($key);
			foreach ($list as $k => $row) {
				if (empty($row['Value']['name'])){
					$id = ltrim($row['Key'] , 'user_');
					$uin_user_info = $user_info[$id];
					if (!empty($uin_user_info)){
						$row['Value']['name'] = $uin_user_info['name'];
						$code = App_Overlord_GameStart::getAlterOverlord('app_overlord_user', $row['Key'], $row['Value']);
					}
				}
			}
		}else{
			return View::error('同步昵称已完成!');
		}
		header('Location: ' . ADMINURL . '/?m=overlord');
	}
	/**
	 * 添加或修改武将界面
	 * Enter description here ...
	 */
	function generals(){
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';

		$smarty = loadSmarty();
		if (!empty($id)){
			loadLib('App_Overlord_Query');
			/**********************查询单个武将**********************/
			$list = App_Overlord_Query::oneCollocate('app_overlord_generals' , $id);
		
			if (empty($list)){
				return View::error('该武将不存在!!!');
			}
			$smarty->assign('list',$list);
		}
		
		loadLib('App_Overlord_Collocate');   
		$data = App_Overlord_Collocate::getSkill();
		
		$smarty->assign('data' , $data);
		return $smarty->fetch('overlord/generals.tpl.php');
	}
	/**
	 * 添加机器人
	 * 
	 */
	function robot(){
		$name_array = array('赵' , '钱' , '孙' , '李' , '周' , '吴' , '郑' , '王' , '冯' , '陈' , '褚' , '卫' , '蒋' , '沈' , '韩' , '杨' , '朱' , '秦' , '尤' , '许' , '何' , '吕' , '施' , '张' , '孔' , '曹' , '严' , '华' , '金' , '魏' , '陶' , '姜' , '戚' , '谢' , '邹' , '喻' , '柏' , '水' , '窦' , '章' , '云' , '苏' , '潘' , '葛' , '奚' , '范' , '彭' , '郎' , '鲁' , '韦' , '昌' , '马' , '苗' , '凤' , '花' , '方' , '俞' , '任' , '袁' , '柳' , '酆' , '鲍' , '史' , '唐' , '费' , '廉' , '岑' , '薛' , '雷' , '贺' , '倪' , '汤' , '滕' , '殷' , '罗' , '毕' , '郝' , '邬' , '常' , '乐' , '于' , '时' , '傅' , '皮' , '卞' , '齐' , '康' , '伍' , '余' , '元' , '卜' , '顾' , '孟' , '平' , '黄');
		$temp = array('邦' , '福' , '歌' , '国' , '和' , '康' , '澜' , '民' , '宁' , '平' , '然' , '顺' , '翔' , '晏' , '宜' , '怡' , '易' , '志' , '然' , '白' , '鸿' , '实' , '彬' , '炳' , '郁' , '斌' , '蔚' , '海' , '波' , '鸿' , '峻' , '波' , '瀚' , '超' , '达' , '厚' , '简' , '明' , '容' , '赡' , '涉' , '实' , '涛' , '文' , '学' , '雅' , '延' , '艺' , '易' , '裕' , '远' , '捷' , '良' , '艺' , '英' , '哲' , '俊' , '和' , '弘' , '化' , '济' , '礼' , '龙' , '仁' , '双' , '天' , '文' , '业' , '益' , '荫' , '周' , '弼' , '恩' , '基' , '教' , '平' , '嗣' , '天' , '望' , '宣' , '颜' , '业' , '悦' , '允' , '运' , '载' , '泽' , '志');
		$name_xing = array('本' , '海' , '厚' , '华' , '辉' , '惠' , '容' , '润' , '寿' , '水' , '馨' , '曜' , '业' , '义' , '庸' , '佑' , '宇' , '元' , '运' , '泽' , '明' , '豪' , '洁' , '捷' , '毅' , '昂' , '岑' , '畅' , '超' , '驰' , '达' , '澹' , '飞' , '芬' , '峯' , '峰' , '歌' , '格' , '寒' , '翰' , '杰' , '洁' , '峻' , '朗' , '丽' , '邈' , '旻' , '明' , '爽' , '兴' , '轩' , '雅' , '扬' , '阳' , '义' , '谊' , '逸' , '懿' , '原' , '远' , '韵' , '卓' , '赫' , '华' , '辉' , '济' , '霁' , '亮' , '临' , '明' , '启' , '熙' , '耀' , '誉' , '远' , '兴' , '源' , '宇' , '玉');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
	//	foreach ($name_rand as $k => $row) {
		//	$name_info[1000+ $k] = $name_array[$row] . $temp[$temp_rand[$k]] . $name_xing[$xing_rand[$k]];
	//	}
		$type = array('wu' , 'wei' ,'shu');
		loadLib('App_Overlord_Query');
		
		/***********************武将列表***********************/
		$generals = App_Overlord_Query::allCollocate('app_overlord_generals');
		//找出名将
		foreach ($generals as $k => $row){
			$row['Value']['id'] = $k;
			//名将
			if ($row['Value']['state'] == 1){
				$generals_state[] = $row;
			}
		}
		$count = 0;
		loadLib('App_Overlord_Collocate');
		//加载技能列表
		$skills = App_Overlord_Collocate::getSkill();
		for ($i = 1001 ; $i < 3200 ; $i++){
			$name_rand = array_rand($name_array , 1);
			$temp_rand = array_rand($temp , 1);
			$xing_rand = array_rand($name_xing , 1);
			$type_rand = array_rand($type , 1);
			$name_info = $name_array[$name_rand] . $temp[$temp_rand] . $name_xing[$xing_rand];
			$num = $i - 1000;
			$lv = floor($i % 72) + 4;
			/***********************初始化人物***********************/
			$user = array('country' => $type[$type_rand] , 'name' => $name_info ,'lv' => $lv ,'exp' => 0 , 'gold' => 10000 , 'diamond' => 1000 , 'exploits' => 5 , 'time' => $time , 'new' => array('num' => 0, 'time' => $time) , 'task' => 0 , 'gift_lv' => 3 , 'login_fate' => array('count' => 1 , 'time' => $time , 'state' => 0));
			$code = App_Overlord_Query::addUser($i, $user);
			
			/***********************初始化武将***********************/
			$generals_rand = array_rand($generals_state);
			$value = $generals_state[$generals_rand]['Value'];
		
			//初始化武将带兵数
			$make_count = 500;
			//初始攻击
			$grow_ap = $value['ap'];
			//初始防御力
			$grow_defense = $value['defense'];
			//初始生命力
			$grow_hp = $value['hp'];
			
			//看下招募过来的武将是否有加兵技能
			if (!empty($value['skills']['strengthen'])){
				foreach ($value['skills']['strengthen'] as $k => $row){
					if ($k == 'staff'){
						$skills_num = $skills['strengthen'][$k][$row[$k]]['num'];
						$make_count += $make_count * $skills_num/100;
						break;
					}
				}
			}
			//武技
			if (!empty($row['skills']['study'])){
				foreach ($row['skills']['study'] as $k => $skills_row){
					if ($k == 'ap'){
						//攻击力加成
						$skills_num = $skills['study'][$k][$skills_row]['num'];
						$grow_ap += $grow_ap * $skills_num/100;
					}elseif ($k == 'defense'){
						//防御力加成
						$skills_num = $skills['study'][$k][$skills_row]['num'];
						$grow_defense += $grow_defense * $skills_num/100;
					}elseif ($k == 'hp'){
						//防御力加成
						$skills_num = $skills['study'][$k][$skills_row]['num'];
						$grow_hp += $grow_hp * $skills_num/100;
					}
				}
			}
			$num = rand(3, 4);
			$is_value[$time] = array('name' => $generals_state[$generals_rand]['Value']['name'], 'generals' => $generals_state[$generals_rand]['Key'] , 'lv' => 1 , 'exp' => 0 , 'ap' => $grow_ap , 'grow_ap' => $value['grow_ap'] , 'defense' => $grow_defense , 'grow_defense' => $value['grow_defense'] , 'hp' => $grow_hp , 'grow_hp' => $value['grow_hp'] , 'make' => array(100 , 100 , 100 , 100) , 'make_count' => $make_count , 'physical' => 10 , 'equipment' => array() , 'skills' => $value['skills'] , 'num' => $num , 'state' => 1 , 'to_uin' => $i , 'time' => $time , 'strategy' => 3 , 'generals_state' => $value['state']);
			$code = App_Overlord_Query::addUin_Generals($i, $is_value);
			
			/***********************初始化城池***********************/
			$city = array('hkee' =>array('lv' => $lv , 'state' => 0 , 'time' => $time , 'to_uin' => $i , 'generals' => $time , 'generals_name' => $generals_state[$generals_rand]['Value']['name']),'assist' =>array(array('lv' => $lv , 'state' => 0 , 'time' => $time , 'to_uin' => 0 , 'generals' => 0))) ;
			$code = App_Overlord_Query::addCity($i, $city);
			
			/***********************初始化兵营***********************/
			$barracks = array(array('type' => 'infantry', 'count' => 500), array('type' => 'cavalryman', 'count' => 500), array('type' => 'toxotae', 'count' => 500), array('type' => 'special', 'count' => 500));
			$code = App_Overlord_Query::addSoldiers($i , $barracks);
		}
		
	}
	/**
	 * 添加或修改武将
	 * Enter description here ...
	 */
	function addgenerals(){
		$id = isset($_POST['id']) ? trim($_POST['id']) : null;
		$name = isset($_POST['name']) ? trim($_POST['name']) : '';
		$hp = isset($_POST['hp']) ? intval($_POST['hp']) : null;
		$ap = isset($_POST['ap']) ? intval($_POST['ap']) : null;
		$defense = isset($_POST['defense']) ? intval($_POST['defense']) : null;
		$state = isset($_POST['state']) ? intval($_POST['state']) : 0;
		$gold = isset($_POST['gold']) ? intval($_POST['gold']) : null;
		$skills = isset($_POST['skills']) ? trim($_POST['skills']) : 0;
		$introduce = isset($_POST['introduce']) ? trim($_POST['introduce']) : '';
		$grow_hp = isset($_POST['grow_hp']) ? trim($_POST['grow_hp']) : null;
		$grow_ap = isset($_POST['grow_ap']) ? trim($_POST['grow_ap']) : null;
		$grow_defense = isset($_POST['grow_defense']) ? trim($_POST['grow_defense']) : null;
		
		if (empty($name)){
			return Format::json_return(1 , '没有填写 武将名称');
		}
		
		if (empty($hp)){
			return Format::json_return(1 , '没有填写  武将生命');
		}
		if (empty($grow_hp)){
			return Format::json_return(1 , '没有填写  武将生命成长值');
		}
		if (empty($ap)){
			return Format::json_return(1 , '没有填写 武将攻击');
		}
		if (empty($grow_ap)){
			return Format::json_return(1 , '没有填写 武将攻击成长值');
		}
		if (empty($defense)){
			return Format::json_return(1 , '没有填写 武将防御');
		}
		if (empty($grow_defense)){
			return Format::json_return(1 , '没有填写 武将防御成长值');
		}
		if (empty($gold)){
			return Format::json_return(1 , '没有填写 武将价格');
		}
		if (empty($introduce)){
			return Format::json_return(1 , '没有填写 武将描述');
		}
		
		if (!empty($skills)){
			$skills_info = explode(',' , $skills);
			$killtype[$skills_info[0]][$skills_info[1]] = $skills_info[2]; 
		}
		if (empty($killtype)){
			$killtype = $skills;
		}
		loadLib('App_Overlord_Query');
		$value = array('name' => $name , 'hp' => $hp , 'grow_hp' => $grow_hp, 'ap' => $ap , 'grow_ap' => $grow_ap, 'defense' => $defense , 'grow_defense' => $grow_defense, 'state' => $state , 'gold' => $gold , 'skills' => $killtype , 'introduce' => $introduce);
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$key = 'generals_'.$time;
		if (!empty($id)){
			/**********************查询个人武将**********************/
			$list = App_Overlord_Query::oneCollocate('app_overlord_generals' , $id);
		}
		if (!empty($list)){
			/**********************修改单个武将**********************/
			
			$code = App_Overlord_Query::alterCollocate('app_overlord_generals', $id, $value);
			$msg = '修改';
		}else{
			/**********************添加武将**********************/
			$code = App_Overlord_Query::addCollocate('app_overlord_generals', $key, $value);
			$msg = '添加';
		}
		if ($code == 0){
			return Format::json_return(0 , $msg.'武将 【'.$name.'】');
		}else{
			return Format::json_return(1 , '写入数据库失败!!!');
		}
	}
	/**
	 * 修改武将绰号
	 */
	function title(){
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		$smarty = loadSmarty();
		if (!empty($id)){
			loadLib('App_Overlord_GameStart');
			/**********************查询单个武将**********************/
			$list = App_Overlord_GameStart::getOneOverlord('app_overlord_generals' , $id);
			
			if (empty($list)){
				return View::error('该武将不存在!!!');
			}
			$smarty->assign('list',$list);
		}
		return $smarty->fetch('overlord/generalstitle.tpl.php');
	}
	/**
	 * 确认修改绰号
	 * Enter description here ...
	 */
	function generalstitle(){
		$id = isset($_POST['id']) ? trim($_POST['id']) : null;
		$name = isset($_POST['name']) ? trim($_POST['name']) : '';
		$title = isset($_POST['title']) ? trim($_POST['title']) : '';
		loadLib('App_Overlord_GameStart');
		if (!empty($id)){
			/**********************查询个人武将**********************/
			$list = App_Overlord_GameStart::getOneOverlord('app_overlord_generals' , $id);
		}
		if (empty($list)) {
			return Format::json_return(1 , '读取武将信息失败');
		}
		$list['Value']['name'] = $name;
		$list['Value']['title'] = $title;
		$code = App_Overlord_GameStart::getAlterOverlord('app_overlord_generals', $id, $list['Value']);
		return Format::json_return(0 , '修改武将 【'.$name.'】绰号成功!');
	}
	/**
	 * 删除武将
	 */
	function delgenerals(){
		$id = isset($_POST['id']) ? trim($_POST['id']) : null;
		
		if(empty($id)) {
			return Format::json_return(1 , '参数错误');
		}

		$Db = loadLib('Db');
		$Db->query("DELETE FROM `app_overlord_generals` WHERE `Key` = '{$id}'");

		return Format::json_return(0 , '删除成功' , $id);
	}
	/**
	 * 任务首页
	 */
	function task(){
		$smarty = loadSmarty();
		
		return $smarty->fetch('overlord/task.tpl.php');
	}
	
	/**
	 * 分库测试
	 */
	function test(){
		set_time_limit(0);
		$temp = 'app_overlord_';
		$table = array($temp . 'barracks' , $temp . 'city' , $temp . 'consume' , $temp . 'exploit_build' , $temp . 'items' , $temp . 'state' , $temp . 'task' , $temp . 'trends' , $temp . 'uin_generals' , $temp . 'user');
		
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		foreach ($table as $k => $row){

			$info = App_Overlord_GameStart::getAllOverlord($row);
			if (!empty($info)){
				
				foreach ($info as $k => $rows){
					if ($row == 'app_overlord_barracks'){
						$uin = ltrim($rows['Key'] , 'barracks_');
						
						App_Overlord_Query::addSoldiers($uin , $rows['Value']);
					}
					if ($row == 'app_overlord_city'){
						$uin = ltrim($rows['Key'] , 'city_');
				
						App_Overlord_Query::addCity($uin , $rows['Value']);
					}
				if ($row == 'app_overlord_consume'){
						$uin = ltrim($rows['Key'] , 'consume_');
													
						App_Overlord_Query::addConsume($uin , $rows['Value']);
					}
				if ($row == 'app_overlord_exploit_build'){
						$uin = ltrim($rows['Key'] , 'exploit_');
					
						App_Overlord_Query::addExploit_Build($uin , $rows['Value']);
					}
				if ($row == 'app_overlord_items'){
						$uin = ltrim($rows['Key'] , 'items_');
				
						App_Overlord_Query::addItems($uin , $rows['Value']);
					}
				if ($row == 'app_overlord_state'){
						$uin = ltrim($rows['Key'] , 'state_');
				
						App_Overlord_Query::addState($uin , $rows['Value']);
					}
				if ($row == 'app_overlord_task'){
						$uin = ltrim($rows['Key'] , 'task_');
						
						App_Overlord_Query::addTask($uin , $rows['Value']);
					}
				if ($row == 'app_overlord_uin_generals'){
						$uin = ltrim($rows['Key'] , 'generals_');
						
						App_Overlord_Query::addUin_Generals($uin , $rows['Value']);
					}
				if ($row == 'app_overlord_user'){
						$uin = ltrim($rows['Key'] , 'user_');
							
						App_Overlord_Query::addUser($uin , $rows['Value']);
				}
			}
			}
		}
		
	
	//	print_r(ltrim($k , '_'));
	
//		$info = app_Overlord_Query::oneUser(9);
	print_r($table);	
	}
}
# end of script